package cn.iocoder.yudao.module.crossborder.order.controller.admin.orderpayment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.order.controller.admin.orderpayment.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.orderpayment.OrderPaymentDO;
import cn.iocoder.yudao.module.crossborder.order.service.orderpayment.OrderPaymentService;

@Tag(name = "管理后台 - 跨境电商订单支付")
@RestController
@RequestMapping("/crossborder/order-payment")
@Validated
public class OrderPaymentController {

    @Resource
    private OrderPaymentService orderPaymentService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商订单支付")
    @PreAuthorize("@ss.hasPermission('crossborder:order-payment:create')")
    public CommonResult<Long> createOrderPayment(@Valid @RequestBody OrderPaymentSaveReqVO createReqVO) {
        return success(orderPaymentService.createOrderPayment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商订单支付")
    @PreAuthorize("@ss.hasPermission('crossborder:order-payment:update')")
    public CommonResult<Boolean> updateOrderPayment(@Valid @RequestBody OrderPaymentSaveReqVO updateReqVO) {
        orderPaymentService.updateOrderPayment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商订单支付")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:order-payment:delete')")
    public CommonResult<Boolean> deleteOrderPayment(@RequestParam("id") Long id) {
        orderPaymentService.deleteOrderPayment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商订单支付")
                @PreAuthorize("@ss.hasPermission('crossborder:order-payment:delete')")
    public CommonResult<Boolean> deleteOrderPaymentList(@RequestParam("ids") List<Long> ids) {
        orderPaymentService.deleteOrderPaymentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商订单支付")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order-payment:query')")
    public CommonResult<OrderPaymentRespVO> getOrderPayment(@RequestParam("id") Long id) {
        OrderPaymentDO orderPayment = orderPaymentService.getOrderPayment(id);
        return success(BeanUtils.toBean(orderPayment, OrderPaymentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商订单支付分页")
    @PreAuthorize("@ss.hasPermission('crossborder:order-payment:query')")
    public CommonResult<PageResult<OrderPaymentRespVO>> getOrderPaymentPage(@Valid OrderPaymentPageReqVO pageReqVO) {
        PageResult<OrderPaymentDO> pageResult = orderPaymentService.getOrderPaymentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderPaymentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商订单支付 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:order-payment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderPaymentExcel(@Valid OrderPaymentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderPaymentDO> list = orderPaymentService.getOrderPaymentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商订单支付.xls", "数据", OrderPaymentRespVO.class,
                        BeanUtils.toBean(list, OrderPaymentRespVO.class));
    }

}